/**
 * Created by Mark on 2018/4/13.
 * 商品列表的逻辑
 */

require("./product-list.css");
require("page/common/nav/nav.js");
require("page/common/header/header.js");
var _product = require("service/product-service.js");
var _mm = require("util/mm.js");
var templateProduct = require("./product.string");
var Pagination = require("util/pagination/pagination.js");/* 导入分页模块 */

var  page = {
    data : {
        listParam : {
            keyword : _mm.getUrlParam("keyword") || "",         //
            categoryId : _mm.getUrlParam("categoryId") || "",
            ownerId : _mm.getUrlParam("ownerId") || "",
            orderBy : _mm.getUrlParam("orderBy") || "",
            pageNum : _mm.getUrlParam("pageNum") || 1,
            pageSize : _mm.getUrlParam("pageSize") || 10
        }
    },
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        this.loadProductList();//加载商品信息
    },
    //1.页面初始化函数
    loadProductList : function() {//1.1加载商品列表
        var listHTML = "";
        var listParam = this.data.listParam;
        var _this = this;
        $productListContent = $(".product-list-con");
        _product.getProductList(listParam,
            function (res) {//成功时的回调函数
                _this.data.listParam.pageNum = 1;
                listHTML = _mm.renderHtml(templateProduct,{
                    list : res.data.list
                });
                $productListContent.html(listHTML);
                _this.loadPagination({//加载分页信息
                    hasPreviousPage : res.data.hasPreviousPage,//是否有上一页，boolean
                    prePage         : res.data.prePage,//上一页的编号
                    hasNextPage     : res.data.hasNextPage,//是否有上一页，boolean
                    nextPage        : res.data.nextPage,//下一页的编号
                    pageNum         : res.data.pageNum,//当前的页号
                    pages           : res.data.pages//总的页数
                });
            }
            ,
            function (err) {//失败时的回调函数
                if(err =="Not Found"){
                    _mm.errorTips("加载信息失败！稍后再试..");
                    return;
                }
                _mm.errorTips(err.msg);
            }
        );
    },
    //2.时间绑定行数
    bindEvent : function(){
        var _this = this;
        //排序的点击事件
        $(".sort-item ").click(function(){
            var $this = $(this);

            if($this.data("type") === "default") {//当前排序为默认排序
                if ($this.hasClass("active")) {
                    return
                } else {
                    $this.addClass("active").siblings(".sort-item")
                        .removeClass("active asc desc");
                    _this.data.listParam.orderBy = "";
                }
            }else if($this.data("type") === "price"){
                $this.addClass("active").siblings(".sort-item").removeClass("active asc desc");
                if(!$this.hasClass("asc")){
                    $this.addClass("asc").removeClass("desc");
                    _this.data.listParam.orderBy = "promotion_price_asc";
                }else{
                    $this.addClass("desc").removeClass("asc");
                    _this.data.listParam.orderBy = "promotion_price_desc";
                }
            }
            //重新加载
            _this.loadProductList();
        });

    },

    //3.辅助函数
    loadPagination : function(pageInfo){//3.1分页
        var _this = this;
        this.pagination ? "" : (this.pagination = new Pagination()); //如果分页对象不存在的话再去实例化出来
        this.pagination.render($.extend({},pageInfo,{
            container : $(".pagination"),
            onSelectPage : function(pageNum){
                _this.data.listParam.pageNum = pageNum;
                _this.loadProductList();
            }
        }));
    },
};

$(function(){
    page.init();
});